//
//  CTRestPasswordView.m
//  ConstellationTalk_0.1
//
//  Created by riley on 15/4/20.
//  Copyright (c) 2015年 hanwei. All rights reserved.
//

#import "CTRestPasswordView.h"
#import "CTDebugManager.h"
#import "CTAppLanguage.h"
#import "CTHeader.h"
#import "UIViewAdditions.h"
#import "CTLoginView_Def.h"

#define CT_RESTPASSWORD_PASSWORD_TEXTFIELD_TAG 1000
#define CT_RESTPASSWORD_RESTPASSWORD_TEXTFIELD_TAG 1001

@interface CTRestPasswordView()<UITextFieldDelegate>
{
    UIView *topView;
    UITextField *passWordTextField;
    UITextField *repeatPWTextField;
}
@end

@implementation CTRestPasswordView
@synthesize delegate;

-(void)dealloc
{
 
    delegate = nil;
    topView = nil;
    passWordTextField = nil;
    repeatPWTextField = nil;
    [CTDebugManager showDebugLogInfo:@"dealloc : CTRestPasswordView Is Removed!!!"];
}

- (instancetype)init
{
    self = [super init];
    if (self)
    {
        self.backgroundColor = CT_HEADER_ORANGE_COLOR;
        self.frame = CGRectMake(0, 0, CT_HEADER_SCREEN_WIDTH, CT_HEADER_SCREEN_HEIGHT);
        
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tapAction:)];
        [self addGestureRecognizer:tap];
        
        [self createRestPassWordTitleView];
        [self createRestPassWordView];
    }
    
    return self;
}
- (void)createRestPassWordTitleView
{
    topView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, CT_HEADER_SCREEN_WIDTH, 64)];
    topView.backgroundColor = CT_HEADER_WHITE_COLOR;
    [self addSubview:topView];
    
    //标题
    UILabel *titleLabel = [[UILabel alloc]initWithFrame:CGRectMake(0, 20, CT_HEADER_SCREEN_WIDTH, 44)];
    titleLabel.font = [UIFont fontWithName:nil size:19];
    titleLabel.text = [CTAppLanguage localizedStringForKey:@"resetPassword_title_text" withValue:nil withTable:@"Language"];
    titleLabel.textColor = CT_HEADER_GREEN_COLOR;
    titleLabel.textAlignment = NSTextAlignmentCenter;
    [topView addSubview:titleLabel];
    
    //返回按钮
    UIButton *backButton = [UIButton buttonWithType:UIButtonTypeCustom];
    backButton.frame = CGRectMake(0, 20, 44, 44);
    [backButton addTarget:self action:@selector(backButtonAction:) forControlEvents:UIControlEventTouchUpInside];
    [backButton setImage:[UIImage imageNamed:CT_HEADER_BACK_IMAGE] forState:UIControlStateNormal];
    [backButton setImage:[UIImage imageNamed:CT_HEADER_BACK_IMAGE_CLICK] forState:UIControlStateHighlighted];
    [topView addSubview:backButton];
    
    //完成
    UIButton *finishButton = [UIButton buttonWithType:UIButtonTypeCustom];
    finishButton.frame = CGRectMake(CT_HEADER_SCREEN_WIDTH-54, 20, 44, 44);
    [finishButton addTarget:self action:@selector(finishButtonAction:) forControlEvents:UIControlEventTouchUpInside];
    finishButton.titleLabel.font = [UIFont systemFontOfSize:15.0f];
    [finishButton setTitleColor:CT_HEADER_GREEN_COLOR forState:UIControlStateNormal];
    [finishButton setTitleColor:CT_HEADER_GREEN_COLOR forState:UIControlStateHighlighted];
    [finishButton setTitle:[CTAppLanguage localizedStringForKey:@"resetPassword_finish_btn_text" withValue:nil withTable:@"Language"] forState:UIControlStateNormal];
    [topView addSubview:finishButton];

    UILabel * topBottomLine = [[UILabel alloc]initWithFrame:CGRectMake(0, topView.height-0.5, CT_HEADER_SCREEN_WIDTH, 0.5)];
    topBottomLine.backgroundColor = CT_HEADER_DEEPLINE_COLOR;
    [topView addSubview:topBottomLine];
}
- (void)createRestPassWordView
{
    UIView *view = [[UIView alloc]initWithFrame:CGRectMake(0, topView.bottom, CT_HEADER_SCREEN_WIDTH,CT_HEADER_SCREEN_HEIGHT-topView.height )];
    view.backgroundColor = CT_HEADER_WHITE_COLOR;
    [self addSubview:view];
    NSString *repeatPWLabel_Text = [CTAppLanguage localizedStringForKey:@"registerView_rest_passWord_text" withValue:nil withTable:@"Language"] ;
    //密码
    UIImageView *passWordImageView = [[UIImageView alloc]initWithFrame:CGRectMake((CT_HEADER_SCREEN_WIDTH-280)/2, 65, 280, 50)];
    passWordImageView.image = [UIImage imageNamed:@"resetting_password_shurukuang"];
    [view addSubview:passWordImageView];
    
    NSString *passWordLabel_text = [CTAppLanguage localizedStringForKey:@"registerView_passWord_text" withValue:nil withTable:@"Language"];
    UILabel *passWordLabel = [[UILabel alloc]initWithFrame:CGRectMake(passWordImageView.origin.x+10, passWordImageView.origin.y, passWordLabel_text.length*15, passWordImageView.height)];
    passWordLabel.font = [UIFont fontWithName:nil size:15];
    passWordLabel.text = [CTAppLanguage localizedStringForKey:@"registerView_passWord_text" withValue:nil withTable:@"Language"];
    passWordLabel.textColor = LOGINVIEW_USER_HEAD_TEXTFIELD_COLOR;
    passWordLabel.textAlignment = NSTextAlignmentLeft;
    [view addSubview:passWordLabel];
    
    passWordTextField = [[UITextField alloc]initWithFrame:CGRectMake(passWordImageView.origin.x+repeatPWLabel_Text.length*15, passWordImageView.origin.y, passWordImageView.width-repeatPWLabel_Text.length*15, 50)];
    passWordTextField.font = [UIFont fontWithName:nil size:15];
    passWordTextField.textColor = LOGINVIEW_USER_TEXTFIELD_COLOR;
    passWordTextField.keyboardType = UIKeyboardTypeDefault;
    passWordTextField.secureTextEntry = YES;
    passWordTextField.placeholder = [CTAppLanguage localizedStringForKey:@"registerView_passWordTextField_text" withValue:nil withTable:@"Language"];
    passWordTextField.tag = CT_RESTPASSWORD_PASSWORD_TEXTFIELD_TAG;
    passWordTextField.textAlignment = NSTextAlignmentLeft;
    passWordTextField.delegate = self;
    [view addSubview:passWordTextField];
    
    //重新输入密码
    //重新输入密码
    UIImageView *repeatPWImageView = [[UIImageView alloc]initWithFrame:CGRectMake(passWordImageView.origin.x, passWordImageView.bottom+15, passWordImageView.width, passWordImageView.height)];
    repeatPWImageView.image = [UIImage imageNamed:@"resetting_password_shurukuang"];
    [view addSubview:repeatPWImageView];
    
//    NSString *repeatPWLabel_Text = [CTAppLanguage localizedStringForKey:@"registerView_rest_passWord_text" withValue:nil withTable:@"Language"] ;
    UILabel *repeatPWLabel = [[UILabel alloc]initWithFrame:CGRectMake(repeatPWImageView.origin.x+10, repeatPWImageView.origin.y, 15*repeatPWLabel_Text.length, repeatPWImageView.height)];
    repeatPWLabel.font = [UIFont fontWithName:nil size:15];
    repeatPWLabel.text = repeatPWLabel_Text;
    repeatPWLabel.textColor = LOGINVIEW_USER_HEAD_TEXTFIELD_COLOR;
//    repeatPWLabel.tag = ;
    repeatPWLabel.textAlignment = NSTextAlignmentLeft;
    [view addSubview:repeatPWLabel];
    
    repeatPWTextField = [[UITextField alloc]initWithFrame:CGRectMake(repeatPWLabel.right, repeatPWImageView.origin.y, repeatPWImageView.width-repeatPWLabel_Text.length*15, 50)];
    repeatPWTextField.font = [UIFont fontWithName:nil size:15];
    repeatPWTextField.textColor = LOGINVIEW_USER_TEXTFIELD_COLOR;
    repeatPWTextField.textAlignment = NSTextAlignmentLeft;
    repeatPWTextField.keyboardType = UIKeyboardTypeDefault;
    repeatPWTextField.secureTextEntry = YES;
    repeatPWTextField.placeholder = [CTAppLanguage localizedStringForKey:@"registerView_rest_passWordTextField_text" withValue:nil withTable:@"Language"];
    repeatPWTextField.tag = CT_RESTPASSWORD_RESTPASSWORD_TEXTFIELD_TAG;
    repeatPWTextField.delegate = self;
    [view addSubview:repeatPWTextField];

}
#pragma mark ButtonAction
//返回
- (void)backButtonAction:(UIButton *) sender
{
    if ([self.delegate respondsToSelector:@selector(thirtPartyRegisterViewBackButton:)])
    {
        [self.delegate thirtPartyRegisterViewBackButton:self];
    }
}
//完成
- (void)finishButtonAction:(UIButton *) sender
{
    [passWordTextField resignFirstResponder];
    [repeatPWTextField resignFirstResponder];
    if ([self.delegate respondsToSelector:@selector(thirtPartyRegisterViewFinishButton:)])
    {
        [self.delegate thirtPartyRegisterViewFinishButton:self];
    }
}
#pragma markd TextFieldDelegate
- (void)textFieldDidBeginEditing:(UITextField *)textField
{
    
}// became first responder

- (void)textFieldDidEndEditing:(UITextField *)textField
{
    
    switch (textField.tag) {
        case CT_RESTPASSWORD_PASSWORD_TEXTFIELD_TAG:
            if ([self.delegate respondsToSelector:@selector(thirtPartyRegisterViewPasswordTextFieldText:registerView:)])
                [self.delegate thirtPartyRegisterViewPasswordTextFieldText:textField.text registerView:self];
            break;
        case CT_RESTPASSWORD_RESTPASSWORD_TEXTFIELD_TAG:
            if ([self.delegate respondsToSelector:@selector(thirtPartyRegisterViewRestPasswordTextFieldText:registerView:)])
                [self.delegate thirtPartyRegisterViewRestPasswordTextFieldText:textField.text registerView:self];
            break;
        default:
            break;
    }
}

- (void)tapAction:(UITapGestureRecognizer *) tap
{
    [passWordTextField resignFirstResponder];
    [repeatPWTextField resignFirstResponder];
}
@end
